/*ProcessManager.java Manager of il2server.exe process 
Copyright (C) 2013  Dmytro Kostiantynovskyi, HartLi

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/
import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.util.Scanner;

public class ProcessManager {
	Process proc;
	public ProcessManager(){

	}
	
	/**
	 * 
	 */
	public void startServer(){
		try {
			ProcessBuilder pb = new ProcessBuilder(Config.serverRootDirectory+Config.serverExecutionFile);
			pb.directory(new File(Config.serverRootDirectory));
			pb.redirectError(Redirect.INHERIT);
			pb.redirectOutput(Redirect.INHERIT);
			proc = pb.start();
			System.err.println("Wait first until SimplexInput is available!!! It may take 20sec. to load up the Simplex Commander.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private boolean input() throws InterruptedException {
		boolean isEnd = false;
		Scanner sc = new Scanner(System.in);
		while(!isEnd){
			System.out.print("\nSimplexInput: ");
			String input = sc.nextLine();
			String[] splittedInput = input.split(":");
			System.out.println("Your input is: "+input);
			Console cons = new Console();
			if(splittedInput.length == 2){
				try {
					cons.writeConsoleMsg(splittedInput[1]);
					Thread.sleep(200);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else if("exit".equals(input)){
				System.out.println("Commander and IL-2 Server will be closed now...");
				try {
					cons.chat("IL-2 server was shutdown by Administrator!", 100, "");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Thread.sleep(1500);
				isEnd = true;
			}
			else if("help".equals(input)){
				System.out.println("exit -- Close commander and il-2 server.\n" +
									"next -- stops current mission on the il-2 server and loads next mission from the rotation file.\n" +
									":[COMMAND] -- sends [COMMAND] to the IL-2 server. Exmaple: :help\n");
			}
			else
				System.err.println("Wrong input. Please, try it again!");
		}
		sc.close();
		return isEnd;
	}
	
	public Runnable input = new Runnable(){
		public void run(){
			try {
				if(input()){
					proc.destroy();
					System.exit(0);
				}
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
}
